<?php

namespace alipay;

use aop\AlipayConfig;
// use AlipayTradeWapPayRequest;    
use aop\request\AlipayTradeWapPayRequest;
use aop\AopClient;
use aop\request\AlipayTradeAppPayRequest;
use aop\request\AlipayTradeCreateRequest;
use stdClass;
use think\Db;
// require_once './extend/aop/AopClient.php';

// require_once './extend/aop/request/AlipayTradeAppPayRequest.php';

class Alipay
{
    public function pay($name,$ooid,$pirce,$notice)
    {
        $s = Db::table('box_setting')->where('id', 1)->find();
        $aop = new AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $s['appId']; //AppID
        //应用私钥
        $aop->rsaPrivateKey = $s['rsaPrivateKey'];
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        //应用公钥
        $aop->alipayrsaPublicKey = $s['alipayrsaPublicKey'];
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $bizcontent = "{\"body\":\"支付\","
            . "\"subject\": \"$name\","
            . "\"out_trade_no\": \"$ooid\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$pirce\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        $request->setNotifyUrl($notice);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        echo json_encode(['code'=>1,'data'=>$response,'ooid'=>$ooid]); //就是orderString 可以直接给客户端请求，无需再做处理。
    }
    
    public function h5pay($name,$ooid,$price,$notice,$page) {
         $s = Db::table('box_setting')->where('id', 1)->find();
         $aop = new AopClient ();
         $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
         $aop->appId = $s['appId'];
         $aop->rsaPrivateKey = $s['rsaPrivateKey'];
         $aop->alipayrsaPublicKey= $s['alipayrsaPublicKey'];
         $aop->apiVersion = '1.0';
         $aop->signType = 'RSA2';
         // $aop->postCharset='GBK';
         $aop->format='json';
         /******必传参数******/
         $object = new stdClass();
         //商户订单号，商家自定义，保持唯一性
         $object->out_trade_no = $ooid;
         //支付金额，最小值0.01元
        //  $object->total_amount = 0.01;
          $object->total_amount = $price;
         //订单标题，不可使用特殊符号
         $object->subject = $name;

         $json = json_encode($object);
         $request = new AlipayTradeWapPayRequest();
         //异步接收地址，仅支持http/https，公网可访问
         $request->setNotifyUrl($notice);
         //同步跳转地址，仅支持http/https
         $request->setReturnUrl($page);
         $request->setBizContent($json);
         $result = $aop->pageExecute($request);
        
         /*$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
         $resultCode = $result->$responseNode->code;
         if(!empty($resultCode)&&$resultCode == 10000){
             echo "成功";
         } else {
             echo "失败";
         }*/
         // echo json_encode(['code'=>1,'data'=>$result,'ooid'=>$ooid]); //就是orderString 可以直接给客户端请求，无需再做处理。
         echo $result;
    }
    
    public function mppay($name,$ooid,$pirce,$notice,$user_id)
    {
        $s = Db::table('box_setting')->where('id', 1)->find();

        $alipayConfig = new AlipayConfig();
        $alipayConfig->setServerUrl("https://openapi.alipay.com/gateway.do");
        $alipayConfig->setAppId($s['appId']);
        $alipayConfig->setPrivateKey($s['rsaPrivateKey']);
        $alipayConfig->setFormat("json");
        $alipayConfig->setAlipayPublicKey($s['alipayrsaPublicKey']);
        $alipayConfig->setCharset("UTF-8");
        $alipayConfig->setSignType("RSA2");
        $aop = new AopClient($alipayConfig);
        $request = new AlipayTradeCreateRequest();
//        $request->setNotifyUrl($notice);
        $appid = $s['appId'];
        $request->setBizContent("{".
            "\"out_trade_no\":\"$ooid\",".
            "\"total_amount\":\"$pirce\",".
            "\"subject\":\"$name\",".
            "\"buyer_id\":\"$user_id\"".
            // "\"op_app_id\":\"$appid\"".
            "\"product_code\":\"JSAPI_PAY\"".
            "}");
        $responseResult = $aop->sdkExecute($request);
        $responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
        $response = $responseResult->$responseApiName;
        halt($response);
//        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
//        echo json_encode(['code'=>1,'data'=>$response,'ooid'=>$ooid]); //就是orderString 可以直接给客户端请求，无需再做处理。
    }
}
